"""
@name : 链表的反转
@author : FanMY_71
@projectname: Pycharm练习
@Time: 2022/6/11-23:07
"""


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None
class Solution:
    def reverseList(self, head):
        pre, cur = None, head
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre
def print_linked_list(head):
    if not head or not head.next:
        return []
    result = []
    while head:
        result.append(head.val)
        head = head.next
    return result
# 创建链表 a->b->c
a = ListNode(5)
b = ListNode(3)
c = ListNode(2)
a.next = b
b.next = c
print(a.val, a.next.val, a.next.next.val)

demo = Solution()
result = demo.reverseList(a)
print(result.val, result.next.val, result.next.next.val)

print(print_linked_list(demo.reverseList(a)))
